﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace Tanks
{
    public enum Direction
    {
        North,South,West,East
    }
    public class Tank
    {
        int walkFrame = 0;
        int walkDuration = 3;
        int reloadFrame = 0;
        Controller controlStrategy;
        List<Weapon> weaps = new List<Weapon>();
        int currWeapIndex=0;
        public Weapon CurrWeap
        {
            get
            {
                return weaps[currWeapIndex];
            }
        }
        public int X
        {
            get
            {
                if (CurrentCell != null)
                {
                    return CurrentCell.X;
                }
                else
                {
                    return -1;
                }
            }
            set
            {
                if ((value >= 0 && value <= game.FieldWidth - 1) && game.Grid[value, Y].Block == BlockType.Empty && game.Grid[value, Y].currTank == null)
                {
                    CurrentCell.currTank = null;
                    CurrentCell = game.Grid[value, Y];
                    CurrentCell.currTank = this;
                }
            }
        }
        public int Y
        {
            get
            {
                if (CurrentCell != null)
                {
                    return CurrentCell.Y;
                }
                else
                {
                    return -1;
                }
            }
            set
            {
                if ((value >= 0 && value <= game.FieldHeight - 1) && game.Grid[X, value].Block == BlockType.Empty && game.Grid[X, value].currTank == null)
                {
                    CurrentCell.currTank = null;
                    CurrentCell = game.Grid[X, value];
                    CurrentCell.currTank = this;
                }
            }
        }
        Bitmap Base = (Bitmap)Bitmap.FromFile("Tank.gif", true);
        Game game = Game.Instance();
        public Cell CurrentCell;
        int health = 100;
        public int Health
        {
            get
            {
                return health;
            }
            set
            {
                health = value;
                if (value < 0)
                {
                    game.Explode(4, CurrentCell,10);
                    CurrentCell.currTank = null;
                    game.RemoveTank(this);
                }
            }
        }
        Direction facing = Direction.East;
        public Direction Facing
        {
            get
            {
                return facing;
            }
        }
        public Tank(int x, int y, Controller C)
        {
            controlStrategy = C;
            controlStrategy.tank = this;
            CurrentCell = game.Grid[x, y];
            CurrentCell.currTank = this;
            weaps.Add(new Gun(this));
            Base.SetResolution(game.Resolution, game.Resolution);
            game.Grid[x, y].Block = BlockType.Empty;
            X = x;
            Y = y;
        }
        public void Fire()
        {
            if (reloadFrame <= 0)
            {
                reloadFrame = CurrWeap.Reload;
                CurrWeap.Fire();
            }
        }
        public bool AddWeap(Weapon w)
        {
            bool find = false;
            for (int i = 0; i < weaps.Count;i++)
            {
                if (weaps[i].GetType() == w.GetType())
                {
                    find = true;
                    break;
                }                
            }
            if (!find)
            {
                weaps.Add(w);
                currWeapIndex = weaps.Count - 1;
            }
            return (!find);
        }
        public void nextWeap()
        {
            currWeapIndex++;
            if (currWeapIndex > weaps.Count - 1)
            {
                currWeapIndex = 0;
            }
        }
        public void Step(Direction dir)
        {
            facing = dir;
            if (walkFrame <= 0)
            {
                walkFrame = walkDuration;
                switch (dir)
                {
                    case Direction.North:
                        Y--;
                        break;
                    case Direction.South:
                        Y++;
                        break;
                    case Direction.West:
                        X--;
                        break;
                    case Direction.East:
                        X++;
                        break;
                }
            }
        }
        public void Draw(Bitmap canvas)
        {
                if (walkFrame > 0)
                {
                    walkFrame--;
                }
                if (reloadFrame > 0)
                {
                    reloadFrame--;
                }
                controlStrategy.Control();
                using (Graphics gr = Graphics.FromImage(canvas))
                {
                    Bitmap Base1 = (Bitmap)Base.Clone();
                    Bitmap GunGr = (Bitmap)CurrWeap.Gr.Clone();
                    switch (facing)
                    {
                        case Direction.North:
                            Base1.RotateFlip(RotateFlipType.Rotate270FlipNone);
                            GunGr.RotateFlip(RotateFlipType.Rotate270FlipNone);
                            break;
                        case Direction.South:
                            Base1.RotateFlip(RotateFlipType.Rotate90FlipNone);
                            GunGr.RotateFlip(RotateFlipType.Rotate90FlipNone);
                            break;
                        case Direction.West:
                            Base1.RotateFlip(RotateFlipType.Rotate180FlipNone);
                            GunGr.RotateFlip(RotateFlipType.Rotate180FlipNone);
                            break;
                    }
                    gr.DrawImage(Base1, new Point(X * game.CellSize, Y * game.CellSize));
                    gr.DrawImage(GunGr, new Point(X * game.CellSize, Y * game.CellSize));
                }
            }
    }
}
